package com.best.config;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author yangruyi
 * @date 2021/3/14
 */
@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("webSecurityManager") SecurityManager securityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager);
        Map<String,String> filterChainMap = new LinkedHashMap<>();
        filterChainMap.put("/select/*","anon");
        filterChainMap.put("/user/login","anon");
        filterChainMap.put("/user/registry","anon");
        filterChainMap.put("/doc/*","anon");
//        filterChainMap.put("/**","authc");
        bean.setFilterChainDefinitionMap(filterChainMap);
        return bean;
    }
    @Bean
    public DefaultWebSecurityManager webSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;
    }
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }
}
